package fr.dauphine.bookstore.modele;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;

/**
 * Auteur.
 */
@Entity
public final class Author extends Persistent {

	/**
	 * Serial version UID.
	 */
	private static final long serialVersionUID = 2596539884083638204L;

	/**
	 * Prenom.
	 */
	private String firstName;

	/**
	 * Nom.
	 */
	private String lastName;

	/**
	 * Livre(s).
	 */
	private List<Book> books = new ArrayList<Book>();

	// Methodes Get&Set

	/**
	 * Retourne les livres.
	 * 
	 * @return Livres
	 */
	@ManyToMany(targetEntity = Book.class, mappedBy = "authors")
	public List<Book> getBooks() {
		return books;
	}

	/**
	 * Met a jour les livres.
	 * 
	 * @param books
	 *            Livres
	 */
	public void setBooks(final List<Book> books) {
		this.books = books;
	}

	/**
	 * Retourne le prenom.
	 * 
	 * @return Prenom
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * Met a jour le prenom.
	 * 
	 * @param firstName
	 *            Prenom
	 */
	public void setFirstName(final String firstName) {
		this.firstName = firstName;
	}

	/**
	 * Retourne le nom.
	 * 
	 * @return Nom
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * Met a jour le nom.
	 * 
	 * @param lastName
	 *            Nom
	 */
	public void setLastName(final String lastName) {
		this.lastName = lastName;
	}

	// Methodes Add

	/**
	 * Ajoute un livre.
	 * 
	 * @param book
	 *            Livre
	 */
	public void addBook(final Book book) {
		book.getAuthors().add(this);
		getBooks().add(book);
	}

	// Surcharges

	/**
	 * @return Prenom et Nom
	 */
	public String toString() {
		return getFirstName() + " " + getLastName();
	}

	/**
	 * Teste l'egalite avec un objet quelconque.
	 * 
	 * @param that
	 *            Objet quelconque
	 * @return Vrai si l'objet that est egal a l'objet courant, faux sinon
	 */
	public boolean equals(final Object that) {
		return (that != null && that instanceof Author && equals((Author) that));
	}
}
